#!/use/bin/python
# coding=utf-8
# 数据分析-主动服务-留存量 7日/15日/30日  (无需执行一次) analysis_retention_number
#KEY
# LOG_DIVICE_ACTIVE_V2_* 为服务当前总订阅量  LOG_DIVICE_NEW_ACTIVE_V2_* 为服务当天订阅量
import redis
import datetime
import pymysql

from dbutils.pooled_db import PooledDB

# proactive_service_conf 数据源
def getConfConnection():
    # 开发环境
    # pool = PooledDB(pymysql, 1, host='172.20.135.96', user='pushdb', passwd='SkYWOrTh$TcOs',
    #                db='proactive_service_conf',
    #                port=3306)  # 1为连接池里的最少连接数
    # 测试环境
    pool = PooledDB(pymysql, 1, host='172.20.154.103', user='test_dmp', passwd='DghHC3lFM1KzT3ZJ',
                db='proactive_service_conf', port=3307)  # 1为连接池里的最少连接数
    # pool = PooledDB(pymysql,1,host='127.0.0.1',user='root',passwd='root',db='life_assistant_data',port=3306) # 5为连接池里的最少连接数
    conn = pool.connection()
    cur = conn.cursor()
    return conn, cur

# proactive_service_data 数据源
def getDataConnection():
    # 开发环境
    #pool = PooledDB(pymysql, 1, host='172.20.151.80', user='pushdb', passwd='SkYWOrTh$TcOs',
    #                db='proactive_service_data',
    #                port=3306)  # 1为连接池里的最少连接数
    # 测试环境
    pool = PooledDB(pymysql, 1, host='172.20.154.103', user='test_dmp', passwd='DghHC3lFM1KzT3ZJ',
                    db='proactive_service_data', port=3407)  # 1为连接池里的最少连接数
    # pool = PooledDB(pymysql,1,host='127.0.0.1',user='root',passwd='root',db='life_assistant_data',port=3306) # 5为连接池里的最少连接数
    conn = pool.connection()
    cur = conn.cursor()
    return conn, cur

# redis
def getReidsConnection():
    redis_info = {
        "host": "172.20.151.90",
        "password": "Coocaa2022",
        "port": 6379,
        "db": 8
    }

    r = redis.Redis(**redis_info, decode_responses=True)
    return r


# 记录 主动服务整体 7/15/30留存量 - 累计版
def saveAllRetentionRate(date,before7Day,before15Day,before30Day):
    try:
        conn, cur = getDataConnection()
        r = getReidsConnection()
        # 判断 analysis_retention_rate 表是否已经存在当天 数据
        selectCountSql = """
                            select count(1) num from analysis_retention_number t where 1=1 and t.ref_date =  '%s' and t.type =1 
                        """ % date
        cur.execute(selectCountSql)
        result = cur.fetchone()
        countNum = result[0]
        #将当天及 7天前的设备差进行比较
        key = "LOG_DIVICE_ACTIVE_V2_ALL_" + date.replace('-','')
        before7Daykey = "LOG_DIVICE_ACTIVE_V2_ALL_" + before7Day.replace('-','')
        before15Daykey = "LOG_DIVICE_ACTIVE_V2_ALL_" + before15Day.replace('-', '')
        before30Daykey = "LOG_DIVICE_ACTIVE_V2_ALL_" + before30Day.replace('-', '')
        #当天处于打开的用户数
        open_num = r.scard(key)
        # N-7日服务处于打开的用户数
        open_num_7 = r.scard(before7Daykey);
        # N-7日服务处于打开的用户数
        open_num_15 = r.scard(before15Daykey);
        # N-7日服务处于打开的用户数
        open_num_30 = r.scard(before30Daykey);
        #N-7日且N日服务处于打开的用户数
        open_7day_num = len(r.sinter(key, before7Daykey))
        # N-15日且N日服务处于打开的用户数
        open_15day_num = len(r.sinter(key, before15Daykey))
        # N-30日且N日服务处于打开的用户数
        open_30day_num = len(r.sinter(key, before30Daykey))

        if countNum == 0:
            logDetail = "累计版-%s日新增-当天处于打开的用户数为%s,7天前为%s,当天且7天前为%s,15天前为%s,当天且15天前为%s,30天前为%s,当天且30天前为%s" % (
                date, open_num, open_num_7, open_7day_num, open_num_15, open_15day_num, open_num_30, open_30day_num)
            print(logDetail)
            # 插入早间服务可见设备量
            insertSql = """insert into analysis_retention_number(retention_num_7day,retention_num_14day,retention_num_30day,ref_date,type) 
                              value(%s,%s,%s,'%s',1)""" % (open_7day_num, open_15day_num, open_30day_num, date)
            cur.execute(insertSql)
        else:
            logDetail = "累计版-%s日更新-当天处于打开的用户数为%s,7天前为%s,当天且7天前为%s,15天前为%s,当天且15天前为%s,30天前为%s,当天且30天前为%s" % (
                date, open_num, open_num_7, open_7day_num, open_num_15, open_15day_num, open_num_30, open_30day_num)
            print(logDetail)
            # 更新早间服务可见设备量
            updateSql = """
                              update analysis_retention_number t set retention_num_7day = %s ,retention_num_14day = %s,retention_num_30day = %s  ,update_time = now()  where
                                  ref_date = '%s' and t.type = 1 
                          """ % (open_7day_num, open_15day_num, open_30day_num, date);
            cur.execute(updateSql)
        saveLog(logDetail, date)
        conn.commit()
    except Exception as e:
        print(e)
    finally:
        cur.close()
        conn.close()

# 记录 主动服务整体 7/15/30留存量 - 新增版
def saveAllNewRetentionRate(date,before7Day,before15Day,before30Day):
    try:
        conn, cur = getDataConnection()
        r = getReidsConnection()
        # 判断 analysis_retention_rate 表是否已经存在当天 数据
        selectCountSql = """
                            select count(1) num from analysis_retention_number t where 1=1 and t.ref_date =  '%s' and t.type = 2 
                        """ % date
        cur.execute(selectCountSql)
        result = cur.fetchone()
        countNum = result[0]
        #将当天及 7天前的设备差进行比较
        key = "LOG_DIVICE_ACTIVE_V2_ALL_" + date.replace('-','')
        before7Daykey = "LOG_DIVICE_NEW_ACTIVE_V2_ALL_" + before7Day.replace('-','')
        before15Daykey = "LOG_DIVICE_NEW_ACTIVE_V2_ALL_" + before15Day.replace('-', '')
        before30Daykey = "LOG_DIVICE_NEW_ACTIVE_V2_ALL_" + before30Day.replace('-', '')
        #当天处于打开的用户数
        open_num = r.scard(key)
        # N-7日服务处于打开的用户数
        open_num_7 = r.scard(before7Daykey);
        # N-7日服务处于打开的用户数
        open_num_15 = r.scard(before15Daykey);
        # N-7日服务处于打开的用户数
        open_num_30 = r.scard(before30Daykey);
        #N-7日且N日服务处于打开的用户数
        open_7day_num = len(r.sinter(key, before7Daykey))
        # N-15日且N日服务处于打开的用户数
        open_15day_num = len(r.sinter(key, before15Daykey))
        # N-30日且N日服务处于打开的用户数
        open_30day_num = len(r.sinter(key, before30Daykey))

        if countNum == 0:
            logDetail = "新增版-%s日新增-当天处于打开的用户数为%s,7天前为%s,当天且7天前为%s,15天前为%s,当天且15天前为%s,30天前为%s,当天且30天前为%s" % (
                  date, open_num, open_num_7, open_7day_num, open_num_15, open_15day_num,open_num_30,open_30day_num)
            print(logDetail)
            # 插入早间服务可见设备量
            insertSql = """insert into analysis_retention_number(retention_num_7day,retention_num_14day,retention_num_30day,ref_date,type) 
                       value(%s,%s,%s,'%s',2)""" % (open_7day_num, open_15day_num, open_30day_num, date)
            cur.execute(insertSql)
        else:
            logDetail = "新增版-%s日更新-当天处于打开的用户数为%s,7天前为%s,当天且7天前为%s,15天前为%s,当天且15天前为%s,30天前为%s,当天且30天前为%s" % (
                date, open_num, open_num_7, open_7day_num, open_num_15, open_15day_num, open_num_30, open_30day_num)
            print(logDetail)
            # 更新早间服务可见设备量
            updateSql = """
                       update analysis_retention_number t set retention_num_7day = %s ,retention_num_14day = %s,retention_num_30day = %s  ,update_time = now()  where
                           ref_date = '%s' and t.type = 2 
                   """ % (open_7day_num, open_15day_num, open_30day_num, date);
            cur.execute(updateSql)
        saveLog(logDetail, date)
        conn.commit()
    except Exception as e:
        print(e)
    finally:
        cur.close()
        conn.close()

# 获取当前日期
def todayYMD():
    today = datetime.datetime.now()-1
    # 获取想要的日期的时间
    re_date = (today).strftime('%Y-%m-%d')
    return re_date


# 获取前1天或N天的日期，beforeOfDay=1：前1天；beforeOfDay=N：前N天
def getdate(beforeOfDay):
    today = datetime.datetime.now()
    # 计算偏移量
    offset = datetime.timedelta(days=-beforeOfDay)
    # 获取想要的日期的时间
    re_date = (today + offset).strftime('%Y-%m-%d')
    return re_date

def saveLog(detail,ref_date):
    try:
        conn, cur = getDataConnection()
        jobId = 53
        jobName = '数据分析-主动服务-留存量 7日/15日/30日'
        tableName = 'analysis_retention_number'
        # 插入日志表
        saveLogSql = """insert into log_xxljob_analysis (job_id,job_name,detail,table_name,ref_date) 
                value(%s,'%s','%s','%s','%s')""" % (jobId,jobName,detail,tableName,ref_date)
        cur.execute(saveLogSql)
        conn.commit()
    except Exception as e:
        print(e)
    finally:
        cur.close()
        conn.close()

if __name__ == '__main__':
    #today = todayYMD()
    date = getdate(1)
    before7Day = getdate(8)
    before15Day = getdate(16)
    before30Day = getdate(31)

    #today = "2021-12-13"
    print ("%s 日期,执行服务留存率(无需执行一次)报表" % date)
    saveAllRetentionRate(date,before7Day,before15Day,before30Day)
    saveAllNewRetentionRate(date,before7Day,before15Day,before30Day)

